package net.atomarrow.db.parser.batch;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import net.atomarrow.db.orm.Attribute;
import net.atomarrow.util.BeanUtil;
import net.atomarrow.util.HtmlUtil;

public class BatchSetter {
    private List<BatchField> fields;
    private int index = -1;

    public BatchSetter(Object object, List<Attribute> attributes) {
        fields = new ArrayList<BatchField>();
        parse(object, attributes);
    }

    private void parse(Object data, List<Attribute> attributes) {

        for (Attribute at : attributes) {
            if (!at.isDbColumn()) {
                continue;
            }
            Serializable value=(Serializable) BeanUtil.getSimpleProperty(data, at.getName());
//            Serializable value=ValueProcessor.processValue((Serializable) BeanUtil.getSimpleProperty(data, at.getName()));
            if(value instanceof String){
                value=HtmlUtil.processScript((String)value);
            }
            fields.add(new BatchField(value, at.getSqlType()));
        }
    }

    public boolean hasNext() {
        if(fields.size()==0){
            return false;
        }
        index++;
        return index < fields.size();
    }

    public BatchField next() {
        return fields.get(index);
    }
    
}
